# -*- coding: utf-8 -*-
import json
from common import log
import os
import time
from project import project_name, case_data


class Config:
    # 节点
    config_path_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'config/config.json'))
    if not os.path.exists(config_path_dir):
        raise FileNotFoundError("请确保config配置文件存在！")
    with open(config_path_dir) as fb:
        data = json.load(fb)
        env = data['execute']['env']
        if project_name:
            data['execute']['project_name'] = project_name
        project_name = data['execute']['project_name']
        base_url = data[project_name][env]['url']
        mark = data['execute']['mark']
        if project_name == "dz_mall":
            base_ip = data[project_name][env]['ip']
        if project_name == 'dz_service':
            if case_data:
                data['execute']['case_data'] = case_data
            case_data = data['execute']['case_data']
    # path
    path_dir = str(os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
    file = path_dir + r'\data\dz_service_case_data.json'

    def __init__(self):
        """
        初始化
        """
        self.log = log.MyLog()
        self.xml_report_path = Config.path_dir + '\\report\\allure_result\\allure_results'
        self.html_report_path = Config.path_dir + '\\report\\allure_result\\allure_report'
        self.report_path = Config.path_dir + '\\report\\allure_result'
        self.report_path_backup = Config.path_dir + '\\report\\report_backup/%s' % time.strftime("%Y.%m.%d.%H.%M.%S")
        self.project_path = Config.path_dir + f'/{self.project_name}'
        self.project_path = Config.path_dir + f'/{self.project_name}'

    @staticmethod
    def get_config(path, case_name=None):
        if not os.path.exists(path):
            raise FileNotFoundError("请确保配置文件存在！")
        try:
            with open(path, "r", encoding='utf-8') as fb:
                data = json.load(fb)
                if case_name:
                    data = data[case_name]
                return data
        except:
            with open(path, "r", encoding='gbk') as fb:
                data = json.load(fb)
                if case_name:
                    data = data[case_name]
                return data


if __name__ == '__main__':
    pass
